package com.wenyu.wenyuyuedu.activity.personage;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.bean.personage.AboutsBean;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.personage.AbousServer;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static anetwork.channel.http.NetworkSdkSetting.context;

/*设置界面的关于*/
public class RegardActivity extends AppCompatActivity {
    @InjectView(R.id.iv_left)
    ImageView ivLeft;
    @InjectView(R.id.regard_logo)
    ImageView regardLogo;
    @InjectView(R.id.regard_name)
    TextView regardName;
    @InjectView(R.id.regard_version)
    TextView regardVersion;
    @InjectView(R.id.regard_gongsiname)
    TextView regardGongsiname;
    @InjectView(R.id.regard_ip_address)
    TextView regardIpAddress;
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regard);
        ButterKnife.inject(this);
        this.activity = RegardActivity.this;
        StatusBarUtil.setStatusBar(activity, R.color.white);
        initView();
        requestData();
    }

    /*初始化控件*/
    private void initView() {
        ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /*初始化控件*/
    private void requestData() {
        String token = AbSharedUtil.getString(activity, SharedConstants.USER_TOKEN);
        AbousServer abousServer = ApiWrapper.getInstance().create(AbousServer.class);
        Call<AboutsBean> abouts = abousServer.getAbouts(token);
        abouts.enqueue(new Callback<AboutsBean>() {
            @Override
            public void onResponse(Call<AboutsBean> call, Response<AboutsBean> response) {
                AboutsBean body = response.body();
                if (body != null) {
                    int code = body.getCode();
                    if (code == 1) {
                        if (body.getInfo() != null) {
                            //图标
                            Glide.with(activity)
                                    .load(Uri.parse(body.getInfo().getLogo()))
                                    .error(R.drawable.login_logo)
                                    .into(regardLogo);
                            //名称
                            regardName.setText(body.getInfo().getIntro());
                            //版本
                            regardVersion.setText("V " + body.getInfo().getVersion());
                        }

                    } else if (code == 0) {
                        showLoginDoalog();
                    }
                }
            }

            @Override
            public void onFailure(Call<AboutsBean> call, Throwable t) {
                MyToast.showToast(activity, "网络异常，请稍后重试", Toast.LENGTH_SHORT);
            }
        });
    }

    android.app.AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    /*显示登陆的dialog*/
    private void showLoginDoalog() {

        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.dailog_login);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(activity, LoginActivity.class);
                startActivity(intent);
                finish();
                ad.dismiss();
            }
        });
    }
}
